package org.visitor.visitorserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.visitor.visitorserver.dto.FormatResponse;
import org.visitor.visitorserver.entity.Admin;
import org.visitor.visitorserver.mapper.AdminMapper;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private FormatResponse formatResponse;

    @GetMapping("/selectAll")
    public FormatResponse selectAdmin() {
        try {
            List<Admin> li = adminMapper.selectList(null);
            formatResponse.setData(li);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

    @PostMapping("/selectById")
    public FormatResponse getOneAdmin(@RequestBody Map<String, String> requestBody) {
        try {
            String id = requestBody.get("id");
            Admin admin = adminMapper.selectById(id);
            System.out.println(admin);
            formatResponse.setData(admin);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

}
